package reviewAlgorithms.dynamicProgramming;

import java.util.Arrays;

public class NumberOfArithmeticSlices {
    public static void main(String[] args) {
        int[] A = {1,2,3,8,9,10};
        System.out.println(numberOfArithmeticSlices(A));
    }
    public static int numberOfArithmeticSlices(int[] A) {
        if (A.length < 3) {
            return 0;
        }
        int sum = 0;
        int end = 0;
        boolean isToEnd = true;
        for (int i = 0; i < A.length - 2; i++) {
            int gap1 = A[i + 1] - A[i];
            for (int j = i + 1; j < A.length; j++) {
                if (A[j] - A[j - 1] != gap1){
                    isToEnd = false;
                    end = j;
                    break;
                }
            }
            if (isToEnd) {
                sum += computeSum(Arrays.copyOfRange(A,i,A.length));
                break;
            }
            if (end - i >= 3){
                sum += computeSum(Arrays.copyOfRange(A,i,end));
                i = end - 1;
                isToEnd = true;
            }
        }
        return sum;
    }

    private static int computeSum(int[] num) {
        int length = num.length;
        return (length - 1)*(length - 2)/2;
    }
}
